package com.whfc.fim.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.fim.dao.FimWarnRuleChannelMapper;
import com.whfc.fim.dao.FimWarnRuleMapper;
import com.whfc.fim.dao.FimWarnRuleUserMapper;
import com.whfc.fim.dto.FimAlgCheckTypeDTO;
import com.whfc.fim.dto.FimAlgTypeDTO;
import com.whfc.fim.dto.FimMsgToUserDTO;
import com.whfc.fim.dto.FimWarnRuleDTO;
import com.whfc.fim.entity.FimWarnRule;
import com.whfc.fim.entity.FimWarnRuleChannel;
import com.whfc.fim.entity.FimWarnRuleUser;
import com.whfc.fim.param.FimSetUserParam;
import com.whfc.fim.service.FimWarnRuleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @DESCRIPTION
 * @AUTHOR GuoDong_Sun
 * @DATE 2020/3/26
 */
@DubboService(interfaceClass = FimWarnRuleService.class, version = "1.0.0")
public class FimWarnRuleServiceImpl implements FimWarnRuleService {

    @Autowired
    private FimWarnRuleMapper fimWarnRuleMapper;

    @Autowired
    private FimWarnRuleUserMapper fimWarnRuleUserMapper;

    @Autowired
    private FimWarnRuleChannelMapper fimWarnRuleChannelMapper;

    @Override
    public PageData<FimWarnRuleDTO> list(Integer deptId, Integer pageNum, Integer pageSize) {

        //初始化默认的报警规则
        int warnRuleCnt = fimWarnRuleMapper.countWarnRuleByDeptId(deptId);
        if (warnRuleCnt == 0) {
            List<FimWarnRule> ruleList = fimWarnRuleMapper.selectDefaultWarnRuleList();
            for (FimWarnRule rule : ruleList) {
                rule.setId(null);
                rule.setDeptId(deptId);
            }
            fimWarnRuleMapper.batchInsert(ruleList);
        }

        //查询报警规则
        PageHelper.startPage(pageNum, pageSize);
        List<FimWarnRuleDTO> list = fimWarnRuleMapper.selectWarnRuleListByDeptId(deptId);
//        PageHelper.clearPage();

        //查询消息渠道和联系人
        for (FimWarnRuleDTO fimWarnRule : list) {
            Integer id = fimWarnRule.getId();
            List<FimMsgToUserDTO> userList = fimWarnRuleUserMapper.selectByWarnRuleId(id);
            List<Integer> msgChannelList = fimWarnRuleChannelMapper.selectMsgChannelByWarnRuleId(id);
            fimWarnRule.setMsgChannelList(msgChannelList);
            fimWarnRule.setUserList(userList);
        }
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public ListData<FimWarnRuleDTO> list(Integer deptId) {
        //查询报警规则
        List<FimWarnRuleDTO> list = fimWarnRuleMapper.selectWarnRuleListByDeptId(deptId);
        return new ListData<>(list);
    }

    @Override
    public Integer selectByDeptIdAndType(Integer deptId, Integer type) {
        return fimWarnRuleMapper.selectByDeptIdAndType(deptId, type);
    }

    @Override
    public List<FimWarnRule> select(FimWarnRule rule) {
        return fimWarnRuleMapper.select(rule);
    }

    @Override
    public int save(FimWarnRule rule) {
        return fimWarnRuleMapper.insertSelective(rule);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setUser(FimSetUserParam request) {

        List<Integer> idList = request.getIdList();
        List<FimMsgToUserDTO> userList = request.getUserList();
        List<Integer> msgChannelList = request.getMsgChannelList();

        for (Integer warnRuleId : idList) {
            //更新消息联系人
            List<FimWarnRuleUser> warnRuleUserList = new ArrayList<>(userList.size());
            for (FimMsgToUserDTO msgToUserDTO : userList) {
                if (StringUtils.isEmpty(msgToUserDTO.getNickName())) {
                    msgToUserDTO.setNickName(msgToUserDTO.getUsername());
                }
                FimWarnRuleUser warnRuleUser = new FimWarnRuleUser();
                warnRuleUser.setToUserId(msgToUserDTO.getUserId());
                warnRuleUser.setToUserName(msgToUserDTO.getNickName());
                warnRuleUser.setToUserPhone(msgToUserDTO.getPhone());
                warnRuleUser.setWarnRuleId(warnRuleId);
                warnRuleUserList.add(warnRuleUser);
            }
            fimWarnRuleUserMapper.deleteByWarnRuleId(warnRuleId);
            if (!warnRuleUserList.isEmpty()) {
                fimWarnRuleUserMapper.batchInsert(warnRuleUserList);
            }

            //更新消息发送方式
            List<FimWarnRuleChannel> warnRuleChannelList = new ArrayList<>();
            for (Integer msgChannel : msgChannelList) {
                FimWarnRuleChannel warnRuleChannel = new FimWarnRuleChannel();
                warnRuleChannel.setWarnRuleId(warnRuleId);
                warnRuleChannel.setMsgchannel(msgChannel);
                warnRuleChannelList.add(warnRuleChannel);
            }
            fimWarnRuleChannelMapper.deleteByWarnRuleId(warnRuleId);
            if (!warnRuleChannelList.isEmpty()) {
                fimWarnRuleChannelMapper.batchInsert(warnRuleChannelList);
            }
        }
    }

    @Override
    public List<FimAlgCheckTypeDTO> listAlgCheckType(Integer deptId) {
        return fimWarnRuleMapper.selectAlgCheckTypeByDeptId(deptId);
    }

    @Override
    public void batchInsertAlgorithmType(List<FimWarnRule> fimWarnRuleList) throws BizException {
        fimWarnRuleMapper.batchInsertAlgorithm(fimWarnRuleList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void algTypeConfigure(Integer deptId, List<FimAlgTypeDTO> list) throws BizException {
        if (deptId == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "组织机构ID不能为空.");
        }
        //删除旧数据
        fimWarnRuleMapper.logicDelByDeptId(deptId);
        //保存算法类型
        for (FimAlgTypeDTO fimAlgTypeDTO : list) {
            FimWarnRule fimWarnRule = new FimWarnRule();
            fimWarnRule.setDeptId(deptId);
            fimWarnRule.setName(fimAlgTypeDTO.getName());
            fimWarnRule.setType(fimAlgTypeDTO.getType());
            fimWarnRuleMapper.insertSelective(fimWarnRule);
        }

    }

    /**
     * 移除删除算法类型
     *
     * @param deptId
     * @param type   算法类型
     */
    @Override
    public void removeAlgorithmByType(Integer deptId, Integer type) {
        fimWarnRuleMapper.logicDelectByDeptIdAndType(deptId, type);
    }

    /**
     * 下行同步项目算法配置类型
     *
     * @param deptId
     * @throws BizException
     */
    @Override
    public List<FimAlgTypeDTO> algTypeList(Integer deptId) throws BizException {
        return fimWarnRuleMapper.selectAlgTypeByDeptId(deptId);
    }
}
